package com.yc.order.biz;

import com.yc.entity.Goodsinfo;
import com.yc.entity.OrderInfo;
import com.yc.order.dao.OrderInfoDao;
import com.yc.utile.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderInfoBizImpl implements OrderInfoBiz{
    @Autowired
    private OrderInfoDao orderInfoDao;
    /**
     *根据用户mno查询所属订单
     * @param mno
     * @return
     */
    @Override
    public List<OrderInfo> findBymno(String mno) {
        List<OrderInfo> bymno = orderInfoDao.findBymno(mno);
        return bymno;
    }
    /**
     *根据用户编号查询所属订单详情个数  不是订单个数哦，是订单内部的个数和
     * @param mno
     * @return
     */
    @Override
    public Map<String, Object> findItemCountByMno(String mno) {
        Map<String, Object> itemCountByMno = orderInfoDao.findItemCountByMno(mno);
        return itemCountByMno;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Map<String, Object> add(String info) {
        Map<String, Object> map = new HashMap<>();
        if(StringUtil.checkNull(info)){
            return map;
        }
        String cnoStr = info.substring(5);
        System.out.println(cnoStr);
        //字符串进行分割
        String[] cnos;
        if (cnoStr.contains(",")) {
            cnos = cnoStr.split(",");
            if (null == cnos || cnos.length <= 0) {
                return map;
            }
        }else{
            cnos = new String[1];
            cnos[0] = info;
        }
        String no = null;
        for (String cno : cnos) {
            no += "'" + cno + "'" + ",";
        }
        String inCno = no.substring(4,no.lastIndexOf(","));
        //生成订单编号
        String ono = System.nanoTime() + "";//获取纳秒
        int result = 0;
        List<Map<String, Object>> list = null;
        if(cnoStr.contains(",")){
            list = orderInfoDao.selectGoodsByCnos(inCno);
        }else {
            String conStr1 = "'" + cnoStr + "'";
            list = orderInfoDao.selectGoodsByCnos(conStr1);
        }
        double totalPrice = 0;
        int num = 0;
        float price = 0;
        int gno=0;
        int i2=0,i3=0 ;
        for (Map<String, Object> m : list) {
            num = Integer.parseInt(m.get("num").toString());
            price = Float.parseFloat(m.get("price").toString());
            totalPrice += num * price;
        }
        int i1 = orderInfoDao.insertOrder(ono, totalPrice);

        for (Map<String, Object> m : list) {
            num = Integer.parseInt(m.get("num").toString());
            price = Float.parseFloat(m.get("price").toString());
            gno = Integer.parseInt(m.get("gno").toString());
            i2 = orderInfoDao.insertOrderItem(ono, gno, num, price);
            i3 = orderInfoDao.updateGoods(num,gno);
        }

        int i4 ;
        if(cnoStr.contains(",")){
            i4 = orderInfoDao.delCart(inCno);
        }else {
            String conStr1 = "'" + cnoStr + "'";
            i4 = orderInfoDao.delCart(conStr1);
        }

        if(i1 != 0 && i2 != 0&& i3 != 0&& i4 != 0){
            result  = 1;
        }else {
            throw new  RuntimeException();
        }
        map.put("code",result);
        map.put("ono",ono);
        return map;
    }
}
